﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using Jalal.Library.Utility;
using System.Web;
using System.IO;

[assembly: TagPrefix("Jalal.Library.Web.Control", "ucc")]
namespace Jalal.Library.Web.Control
{
    [SupportsEventValidation, ValidationProperty("FileName")]
    [ToolboxData("<{0}:ImageUploader runat=server></{0}:ImageUploader>")]
    public class ImageUploader : FileUpload
    {
        private int _width;
        public int ImageWidth
        {
            get { return this._width; }
            set { this._width = value; }
        }

        private int _height;
        public int ImageHeight
        {
            get { return this._height; }
            set { this._height = value; }
        }

        private string _tempFolder;
        public string TempFolder
        {
            get { return this._tempFolder; }
            set { this._tempFolder = value; }
        }

        private string _uploadFolder;
        public string UploadFolder
        {
            get { return this._uploadFolder; }
            set { this._uploadFolder = value; }
        }

        private List<Size> _sizes;
        public List<Size> Sizes
        {
            get { return this._sizes; }
            set { this._sizes = value; }
        }

        public ImageUploader()
        {
            this.EnableViewState = false;
        }

        public List<string> UploadImage()
        {
            List<string> imageFiles = null;
            if (Page.IsPostBack)
            {
                if (HasFile)
                {
                    string tempFilePath = UploadFileToTemp();
                    imageFiles = new List<string>();

                    if (Sizes != null && Sizes.Count > 0)
                    {
                        foreach (Size size in Sizes)
                        {
                            imageFiles.Add(CreateThumbnailFromTempFile(size, tempFilePath));
                        }
                    }
                    else
                    {
                        Size size = new Size(ImageWidth, ImageHeight);
                        imageFiles.Add(CreateThumbnailFromTempFile(size, tempFilePath));
                    }
                    DeleteTempFile(tempFilePath);
                } 
            }
            return imageFiles;
        }

        private string MakeFullTempPath()
        {
            string fullPath = TempFolder;
            if (!TempFolder.EndsWith("/"))
                fullPath += "/" + FileName;
            else
                fullPath += FileName;
            return fullPath;
        }

        private string MakeFullOutputPath()
        {
            string fullPath = UploadFolder;
            if (!TempFolder.EndsWith("/"))
                fullPath += "/" + FileName;
            else
                fullPath += FileName;
            return fullPath;
        }

        private string UploadFileToTemp()
        {
            string fullPath = MakeFullTempPath();
            if (File.Exists(HttpContext.Current.Server.MapPath(fullPath)))
                FileAccessUtility.DeleteFile(fullPath);
            SaveAs(HttpContext.Current.Server.MapPath(fullPath));
            return fullPath;
        }

        private void DeleteTempFile(string tempFilePath)
        {
            FileAccessUtility.DeleteFile(tempFilePath);
        }

        private string CreateThumbnailFromTempFile(Size size, string tempFilePath)
        {
            if (size.Width > 0 && size.Height > 0)
            {
                string suffix = "_" + Guid.NewGuid().ToString().Replace("-", "") + "_W" + size.Width.ToString() + "xH" + size.Height.ToString();
                return ImageResizer.CreateThumbnail(tempFilePath, MakeFullOutputPath(), size.Width,
                    size.Height, true, suffix);
            }
            else if (size.Width > 0)
            {
                string suffix = "_" + Guid.NewGuid().ToString().Replace("-", "") + "_W" + size.Width.ToString() + "xH0";
                return ImageResizer.CreateThumbnail(tempFilePath, MakeFullOutputPath(), size.Width,
                    true, suffix);
            }
            else if (size.Height > 0)
            {
                string suffix = "_" + Guid.NewGuid().ToString().Replace("-", "") + "_W0xH" + size.Height.ToString();
                return ImageResizer.CreateThumbnailByHeight(tempFilePath, MakeFullOutputPath(), size.Height,
                    true, suffix);
            }
            else
                return null;
        }
    }
}
